package com.server.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.server.date.DateType;
import com.server.date.DateUtil;
import com.server.service.ICommonDataService;
import com.server.service.IExaminationService;

/**
 * @author wufeng
 * @date 创建时间：2016年8月19日 下午4:01:10 
 */
@Service("ExaminationService")
public class ExaminationService implements IExaminationService{
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private ICommonDataService commonDataService;
	
	@Override
	public Map<String,Object> getMapList(String user,Map<String, Object> map) {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		Map<String, Object> map2 = new HashMap<String, Object>();
		StringBuffer sqlBuffer = new StringBuffer();
		StringBuffer sqlBuffer2 = new StringBuffer();
		String type = map.get("type").toString();
		String value=""; 
		Object value2 = map.get("value");
		if(!StringUtils.isEmpty(value2)){
			value = value2.toString();
		}
		String postion = map.get("postion").toString();
		String pageSize = map.get("pageSize").toString();
		sqlBuffer.append("select * from education.v_exam where valid = 1");
		sqlBuffer2.append("select count(id) AS sum from education.v_exam where valid = 1");
		if(!type.equals("0")){
			sqlBuffer.append(" and class_id = '").append(type).append("'");
			sqlBuffer2.append(" and class_id = '").append(type).append("'");
		}
		if(value != null&&value.length()>0){
			sqlBuffer.append(" and name like '%").append(value).append("%'");
			sqlBuffer2.append(" and name like '%").append(value).append("%'");
		}
		sqlBuffer2.append(" and state = 5");
		sqlBuffer.append(" and state = 5 order by createTime desc");
		sqlBuffer.append(" limit ").append(postion).append(",").append(pageSize);
		try {
			list = jdbcTemplate.queryForList(sqlBuffer.toString());
			map2 = jdbcTemplate.queryForMap(sqlBuffer2.toString());
		} catch (Exception e) {
			return new HashMap<String, Object>();
		}
		if(list.size()>0){
			for (Map<String, Object> map3 : list) {
				List<Map<String, Object>> list2 = new ArrayList<Map<String,Object>>();
				String id = map3.get("id").toString();
				String sql = "select id AS mId, state AS mstate, score As mscore from education.mark where examId = '"+id+"' and nickname = '"+user+"'";
				try {
					list2 = jdbcTemplate.queryForList(sql);
					if(list2.size()>0){
						map3.put("mstate", list2.get(0).get("mstate"));
						map3.put("mId", list2.get(0).get("mId"));
						map3.put("mscore", list2.get(0).get("mscore"));
						
					}else {
						map3.put("mstate", null);
						map3.put("mId", null);
						map3.put("mscore", null);
					}
				} catch (Exception e) {
					continue;
				}
			}
		}
		map2.put("list", list);
		return map2;
		
	}
	
	@Override
	public boolean submit(String id, Map<String, Object> inputMap,
			String operator) {
		String sql = "select state from education.mark where id='"+id+"'";
		try {
			int state = jdbcTemplate.queryForObject(sql, Integer.class);
			if(state == 1){
				return false;
			}
		} catch (Exception e) {
			return false;
		}
		String dateStr = DateUtil.getDateStr(new Date(),DateType.YYYY_MM_DD_HH_mm_ss_SSS); 
		inputMap.put("submitTime", dateStr);
		boolean isSuccess = commonDataService.update(operator, "education.mark", "id", id, inputMap);
		return isSuccess;
	}
	@Override
	public boolean updateStartTime(String id){
		boolean isSuccess = true;
		String dateStr = DateUtil.getDateStr(new Date(),DateType.YYYY_MM_DD_HH_mm_ss_SSS);
		String sql = "update education.mark set answerTime = '%s' where id = %s";
		try {
			int update = jdbcTemplate.update(String.format(sql,dateStr,id));
			if(update <= 0){
				isSuccess = false;
			}
		} catch (Exception e) {
			isSuccess = false;
		}
		return isSuccess;
	}
	@Override
	public Map<String, Object> getMap(Integer id) {
		String sql = "select * from education.mark where id='"+id+"'";
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map = jdbcTemplate.queryForMap(sql);
		} catch (Exception e) {
			return map;
		}
		return map;
	}

	@Override
	public boolean addPapers(String userName, Integer courseId) {
		boolean isSuccess = true;
		List<Map<String, Object>> relistList = list(courseId);
		for (Map<String, Object> map : relistList) {
			Object object=map.get("id");
			if(!StringUtils.isEmpty(object)){
				String sql = "insert into education.mark (nickname,examId,state,isCorrect) "
						+ "values('"+userName+"','"+object.toString()+"','0','0')";
				try {
					int update = jdbcTemplate.update(sql);
					if(update < 1){
						isSuccess = false;
					}
				} catch (Exception e) {
					isSuccess = false;
				}
				
			}
		}
		return isSuccess;
	}
	
	@Override
	public Map<String, Object> addPaper(String userName, Integer examId) {
		if(userName == null||userName.length() < 0){
			return new HashMap<String, Object>();
		}
		String sql = "select * from education.mark where nickname = '"+userName+"'"
				+ "and examId = '"+examId+"'";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		if(list.size() > 0){
			return new HashMap<String, Object>();
		}
		Map<String, Object> inputParamMap = new HashMap<String, Object>();
		inputParamMap.put("nickname", userName);//VARCHAR
		inputParamMap.put("examId", examId);//VARCHAR
		Map<String, Object> execute = new SimpleJdbcCall(jdbcTemplate).withCatalogName("education")
			.withProcedureName("P_MARK_ADD")
			.declareParameters(
						new SqlParameter("nickname",java.sql.Types.VARCHAR),
						new SqlParameter("examId",java.sql.Types.VARCHAR),
						new SqlOutParameter("id",java.sql.Types.INTEGER)
			).execute(inputParamMap);
		return execute;	
	}

	@Override
	public List<Map<String, Object>> list(Integer course_id) {
		String sql ="select id from education.exam where FIND_IN_SET('"+course_id+"',courseId)";
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = jdbcTemplate.queryForList(sql);
		} catch (Exception e) {
			return new ArrayList<Map<String,Object>>();
		}
		return list;
	}
	@Override
	public Map<String, Object> geSum(){
		String sql = "select count(id) AS sum from education.v_mark_exam where valid=1";
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map = jdbcTemplate.queryForMap(sql);
			} catch (Exception e) {
			return new HashMap<String, Object>();
		}
		return map;
	}
	
}
